const conn = require("../dataMob");


// // 跳转页面
module.exports.index = (req, res) => {
  res.render("/MOB/index");
};
module.exports.advan = (req, res) => {
  res.render("MOB/advan");
};
module.exports.flow = (req, res) => {
  res.render("/MOB/flow");
};
module.exports.gie = (req, res) => {
  res.render("/MOB/gie");
};
module.exports.intro = (req, res) => {
  res.render("/MOB/intro");
};
module.exports.mejoin = (req, res) => {
  res.render("/MOB/mejoin");
};
module.exports.scheme = (req, res) => {
  res.render("/MOB/scheme");
};
module.exports.term = (req, res) => {
  res.render("/MOB/term");
};
module.exports.dynamic = (req, res) => {
  res.render("/MOB/news/dynamic");
};
module.exports.infor = (req, res) => {
  res.render("/MOB/news/infor");
};
module.exports.oper = (req, res) => {
  res.render("/MOB/org/oper");
};

// 问题切换
module.exports.issue_tab = (req, res) => {
  conn.query(
    "select * from page_news where page_options_id=? limit 0,10",
    req.query.nav_id,
    (err, data) => {
      if (err) return console.log(err);
      res.json(data);
    }
  );
};

// 新闻 数据渲染
module.exports.news_show = (req, res) => {
  pageNum = req.query.pageNum;
  pageSize = req.query.pageSize;
  index = (pageNum - 1) * pageSize;
  conn.query(
    `select count(*) total from page_news where page_options_id = (select id from pc_nav2 where href=?);select * from page_news where page_options_id = (select id from pc_nav2 where href=?) limit ${index},${pageSize}`,
    [req.query.href, req.query.href],
    (err, data) => {
      if (err) return console.log(err);
      for (i = 0; i < data[1].length; i++) {
        text = "";
        for (j = 0; j < 34; j++) {
          text += data[1][i].matter[j];
        }
        data[1][i].matter = text;
      }
      res.json({
        pageSize: pageSize,
        data: data[1],
        total: data[0][0].total
      });
    }
  );
};

// 加盟指南 数据渲染
module.exports.gie_show = (req, res) => {
  conn.query(
    `select * from pc_nav2 where nav_id=(select nav_id from pc_nav2 where href=?) and none<>2;select * from page_news where page_options_id in (select id from pc_nav2 where nav_id = (select nav_id from pc_nav2 where href=?))`,
    [req.query.href, req.query.href],
    (err, data) => {
      if (err) return console.log(err);
      res.json(data);
    }
  );
};

// 所在位置 渲染
module.exports.position = (req, res) => {};

// 文章返回
module.exports.article = (req, res) => {
  conn.query(
    "select * from page_news where id=?",
    req.params.id,
    (err, data) => {
      if (err) return console.log(err);
      res.render("article", { data: data[0] });
    }
  );
};
